/*
============================================================================
 Name        : Ticker.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CTicker implementation
============================================================================
*/

#include "Ticker.h"
#include "PluginUtility.h"

#include <eikenv.h>			//CEikonEnv

_LIT(KTickerHyphen, "  -  ");

CTicker::CTicker(MPluginContainer* aContainer)
: iContainer(aContainer), iTimerTick(&TimerTick, this), iPos(0), iWidth(0)
{
}

EXPORT_C CTicker::~CTicker()
{
	if (iTimer->IsActive()) iTimer->Cancel();
	DELETE(iTimer);
}

EXPORT_C CTicker* CTicker::NewLC(MPluginContainer* aContainer)
{
	CTicker* self = new (ELeave)CTicker(aContainer);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

EXPORT_C CTicker* CTicker::NewL(MPluginContainer* aContainer)
{
	CTicker* self=CTicker::NewLC(aContainer);
	CleanupStack::Pop(); // self;
	return self;
}

void CTicker::ConstructL()
{
	if (!iTimer) iTimer = CPeriodic::New(EPriorityLow);
}

void CTicker::PreSetText(TInt aWidth)
{
	iWidth = aWidth;

	iTickerText.Zero();
	iPos = -5;

	//Deactivate timer
	if (iTimer->IsActive()) iTimer->Cancel();
}

void CTicker::PostSetText()
{
	//Check if moving is needed
	TInt textWidth = CEikonEnv::Static()->NormalFont()->TextWidthInPixels(iTickerText);
	if (textWidth > iWidth)
	{
		//Ticker moving is needed
		iTickerText.Append(KTickerHyphen);
		if (!iTimer->IsActive()) iTimer->Start(500000, 200000, iTimerTick);
	}

	//Refresh ticker moving
	iMovingText.Copy(iTickerText);

	//Repaint
	iContainer->Repaint();
}

EXPORT_C void CTicker::SetText(const TDesC& aText, TInt aWidth)
{
	if (!aWidth) return;

	//Pre-process
	PreSetText(aWidth);
	
	//Set text
	iTickerText.Copy(aText);

	//Post-process
	PostSetText();
}

EXPORT_C void CTicker::SetTextFormat(TRefByValue<const TDesC16> aFmt, TInt aWidth, ...)
{
	//Pre-process
	PreSetText(aWidth);
	
	//Set text
	iTickerText.Zero();
	iTickerText.AppendFormat(aFmt);

	//Post-process
	PostSetText();
}

TInt CTicker::TimerTick(TAny *obj)
{
	CTicker* self = (CTicker*)obj;

	TInt length = self->iTickerText.Length();
	if (length == 0) return 0;

	if (++self->iPos >= length) self->iPos = 0;
	TInt pos = self->iPos;

	self->iMovingText.Copy(self->iTickerText.Right(length - pos));
	if (pos > 0) self->iMovingText.Append(self->iTickerText.Left(pos));

	//Repaint
	self->iContainer->Repaint();

	return 1;
}

EXPORT_C const TDesC& CTicker::GetText()
{
	return iMovingText;
}

EXPORT_C TInt CTicker::Length()
{
	return iMovingText.Length();
}

EXPORT_C void CTicker::Activate()
{
	TInt pos = iTickerText.Find(KTickerHyphen);
	if (pos == KErrNotFound) return;

	TInt length = iTickerText.Length() - 5;
	if (pos < length) return;

	//Reactivate timer
	SetText(iTickerText.Left(length), iWidth);
}

EXPORT_C void CTicker::Deactivate()
{
	//Cancel timer
	if (iTimer->IsActive()) iTimer->Cancel();
}
